package com.example.webwork.controller;


import com.example.webwork.entity.Contact;
import com.example.webwork.service.IContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/contacts")
public class ContactController {

    @Autowired
    @Qualifier("contactServiceImpl")
    private IContactService contactService;

    @GetMapping("/user")
    public List<Contact> getAllContacts(String username) {
        System.out.println(username);
        System.out.println(contactService.getAllContacts(username));
        return contactService.getAllContacts(username);
    }

    @PostMapping("/save")
    public String saveContact(@RequestBody Contact contact) {
        System.out.println(contact);
        contactService.saveContact(contact);
        return "Contact saved successfully";
    }

    @PutMapping
    public String updateContact(@RequestBody Contact contact) {
        contactService.updateContact(contact);
        return "Contact updated successfully";
    }

    @PostMapping ("/del")
    public String deleteContact(@RequestBody Contact contact) {
        System.out.println(contact);
        contactService.deleteContact(contact);
        return "Contact deleted successfully";
    }

    @GetMapping("/search")
    public List<Contact> searchContacts(@RequestParam String username,
                                        @RequestParam String name) {
        return contactService.searchContacts(username, name);
    }
}